﻿//using System.IO;
//using System.Runtime.Serialization.Json;
//using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;

namespace AgileBoard.Web.Controllers.Util
{
    public static class ExtJsJsonHelper
    {
        public static string ToExtJsJsonReader(this object data, string rootName)
        {
            return data.ToString(); //.ToJson(); //string.Format("{{ {0} : {1} }}", rootName, data.ToJson());
        }

        public static string ToJson(this object data)
        {
            var serializer = new JavaScriptSerializer();


            //string str = "[ { text: 'ExtJS Main', leaf: true }, { text: 'ExtJS Learn', leaf: true },{ text: 'ExtJS Forum', children: [ { text: 'Premium Help', leaf: true }, { text: 'Examples and Extras', leaf: true } ] } ] ";
            return serializer.Serialize(data);
        }
    }

    public static class ExtJsJsonHelperExt
    {
        public static string ToExtJsJson(this IEnumerable collection, string rootName)
        {
            var serializer = new DataContractJsonSerializer(collection.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, collection);

                var sb = new StringBuilder();

                sb.Append("{" + rootName + ":");

                sb.Append(Encoding.Default.GetString(ms.ToArray()));

                sb.Append("}");

                return sb.ToString();
            }
        }

        public static string ToExtJsJson(this object item)
        {
            var serializer = new DataContractJsonSerializer(item.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, item);

                var sb = new StringBuilder();

                //sb.Append("{" + rootName + ":");

                sb.Append(Encoding.Default.GetString(ms.ToArray()));

                //sb.Append("}");

                return sb.ToString();
            }
        }


        public static string ToExtJsJsonItem(this object item, string rootName)
        {
            var serializer = new DataContractJsonSerializer(item.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, item);

                var sb = new StringBuilder();

                sb.Append("{success:true," + rootName + ":");

                sb.Append(Encoding.Default.GetString(ms.ToArray()));

                sb.Append("}");

                return sb.ToString();
            }
        }

        public static string ToExtJsJsonItem(this object item)
        {
            var serializer = new DataContractJsonSerializer(item.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, item);

                var sb = new StringBuilder();


                sb.Append(Encoding.Default.GetString(ms.ToArray()));


                return sb.ToString();
            }
        }


        public static string ToExtJsJson(this IEnumerable collection, string rootName, string countName)
        {
            var c = (ICollection) collection;


            var serializer = new DataContractJsonSerializer(c.GetType());

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, c);

                var sb = new StringBuilder();

                sb.Append("{" + countName + ":" + c.Count + ",");

                sb.Append(rootName + ":");

                sb.Append(Encoding.Default.GetString(ms.ToArray()));

                sb.Append("}");

                return sb.ToString();
            }
        }


        public static T FromJsonTo<T>(this string jsonString)
        {
            var ser = new DataContractJsonSerializer(typeof (T));


            var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));


            var jsonObject = (T) ser.ReadObject(ms);


            return jsonObject;
        }
    }
}